package com.zmn.plat.dubbo.impl.orderconfig.cancel;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.cancel.reason.OrderCancelReasonDIO;
import com.zmn.plat.common.dto.orderconfig.cancel.reason.OrderCancelReasonDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.cancel.OrderCancelReasonListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelCategory;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelReason;
import com.zmn.plat.model.entity.orderconfig.cancel.OrderCancelReasonQuery;
import com.zmn.plat.model.vo.orderconfig.cancel.OrderCancelReasonVO;
import com.zmn.plat.services.interfaces.orderconfig.cancel.OrderCancelCategoryService;
import com.zmn.plat.services.interfaces.orderconfig.cancel.OrderCancelReasonService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description：订单取消缘由
 * @since 2019/10/21 18:17
 * @modifier
 **/
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class OrderCancelReasonListRemoteServiceImpl implements OrderCancelReasonListRemoteService {
    private static final String TAG = "订单取消缘由 dubbo 接口";
    @Resource
    private OrderCancelReasonService orderCancelReasonService;
    @Resource
    private OrderCancelCategoryService orderCancelCategoryService;

    /**
     * 描述: 根据主键查询取消缘由
     *
     * @param id
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<OrderCancelReasonDRO> getById(Integer id) {
        log.info("getById：id:{},", id);

        OrderCancelReason reason = orderCancelReasonService.findByKey(id);
        if (reason == null) {
            log.error(String.format("[%s]参数:[%s]对应的对象不存在", TAG, id));
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        OrderCancelReasonDRO dro = new OrderCancelReasonDRO();
        BeanCopyUtils.copyProperties(reason, dro);

        OrderCancelCategory cancelCategory = orderCancelCategoryService.findByKey(reason.getCategId());
        if (!Objects.isNull(cancelCategory)) {
            dro.setCategName(cancelCategory.getName());
        }

        dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));
        dro.setNodeName(ProductDict.getCancelNodeStr(dro.getNode()));
        dro.setTypeName(ProductDict.getCancelTypeStr(dro.getType()));
        return ResponseDTO.success(dro);
    }

    /**
     * 描述: 根据条件查询取消缘由
     *
     * @param orderCancelReasonDIO
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<OrderCancelReasonDRO>> listByQuery(OrderCancelReasonDIO orderCancelReasonDIO) {
        log.info("listByQuery：orderCancelReasonDIO:{},", orderCancelReasonDIO);

        OrderCancelReasonQuery query = new OrderCancelReasonQuery();
        BeanCopyUtils.copyProperties(orderCancelReasonDIO, query);
        List<OrderCancelReasonVO> reasons = Optional.ofNullable(orderCancelReasonService.listByQuery(query)).orElse(Collections.emptyList());
        BeanCopier copier = BeanCopier.create(OrderCancelReasonVO.class, OrderCancelReasonDRO.class, false);
        List<OrderCancelReasonDRO> result = reasons.stream().map(reason -> {
            OrderCancelReasonDRO dro = new OrderCancelReasonDRO();
            copier.copy(reason, dro, null);
            dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));
            dro.setNodeName(ProductDict.getCancelNodeStr(dro.getNode()));
            dro.setTypeName(ProductDict.getCancelTypeStr(dro.getType()));
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(result);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listVtByStatus(Integer status) {
        log.info("listVtByStatus：status:{},", status);

        OrderCancelReasonQuery query = new OrderCancelReasonQuery();
        query.setStatus(status);
        List<OrderCancelReasonVO> list = Optional.ofNullable(orderCancelReasonService.listByQuery(query)).orElse(Collections.emptyList());

        List<VtDTO> items = list.stream().map(item -> new VtDTO(item.getId(), item.getReason(), item.getStatus())).collect(Collectors.toList());

        return ResponseDTO.success(items);
    }

    @Override
    public ResponseDTO<List<VtDTO>> listLevelVtByStatus(Integer status, Integer level) {
        log.info("listLevelVtByStatus：status:{},level:{}", status, level);

        OrderCancelReasonQuery query = new OrderCancelReasonQuery();
        query.setStatus(status);
        List<OrderCancelReasonVO> list = Optional.ofNullable(orderCancelReasonService.listByQuery(query)).orElse(Collections.emptyList());

        List<VtDTO> items = new ArrayList<>();
        if (Objects.equals(level, GlobalConsts.LEVEL_1)) {
            items.addAll(list.stream().map(item -> new VtDTO(item.getId(), item.getLevelOneReason(), item.getStatus())).collect(Collectors.toList()));
        } else if (Objects.equals(level, GlobalConsts.LEVEL_2)) {
            items.addAll(list.stream().map(item -> new VtDTO(item.getId(), item.getReason(), item.getStatus())).collect(Collectors.toList()));
        } else {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_ORDERCONFIG_NOT_SUPPORT_LEVEL);
        }

        return ResponseDTO.success(items);
    }
}
